package jjoms.model.item;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import jjoms.model.member.MemberVO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ItemServiceImpl implements ItemService {

	 @Resource(name = "itemDAOImpl")
	   private ItemDAO itemDAO;
	 
	 @Resource(name="uploadPath")
		private String path;
	 
	@Override
	public List<ItemVO> getItemList(String keyword) {
		return itemDAO.getItemList(keyword);
	}
	 
	@Override
	public void register(ItemVO vo) {
		System.out.println("Service : "+vo);
		MultipartFile mFile = vo.getUploadFile();
		if(mFile!=null&&!mFile.isEmpty()){
			
			File copyFile = new File(path+vo.getMemberId()+"\\"+mFile.getOriginalFilename());
			copyFile.getParentFile().mkdirs();
			try{
				mFile.transferTo(copyFile);
				System.out.println("file upload ok~~");
				vo.setFileDir(mFile.getOriginalFilename());
			//	String originPath = vo.getFileDir();
			//	System.out.println("originPath : " + originPath);
			//	vo.setFileDir(vo.getMemberId()+"\\"+originPath);
				System.out.println(vo);
				itemDAO.register(vo);
				System.out.println("file regist ok~");
			}catch(IllegalStateException  | IOException e){
				e.printStackTrace();
			}
		}
	}

	@Override
	public ItemVO findItemByName(String name) {
		return itemDAO.findItemByName(name);
	}

	@Override
	public ItemVO getItemContent(int itemNo) {
			ItemVO ivo = itemDAO.getItemContent(itemNo);
			String category = ivo.getCategory();
			System.out.println("category" + category);
			System.out.println("set전 ivo " + ivo);
			CategoryBean cb = new CategoryBean(category);
			ivo.setcBean(cb);
			System.out.println("set후 ivo " + ivo);
			
			System.out.println("**");
			
		return ivo;
	}

	@Override
	public void update(ItemVO vo) {
		itemDAO.update(vo);
	}

	@Override
	public void delete(int itemNo) {
		itemDAO.delete(itemNo);
	}


	@Override
	public HashMap getCategory() {
		HashMap<String,ArrayList> map = new HashMap<String,ArrayList>();
		ArrayList<String> category = (ArrayList<String>) itemDAO.getCategory();
		
		
		for(int i=0; i<category.size(); i++){
				ArrayList<String> division = (ArrayList<String>) itemDAO.getDivision(category.get(i));
				map.put(category.get(i),division );
		}
		System.out.println("service map : "+ map);
		return map;
	}

	@Override
	public HashMap fileDownload(String filename) {
		return null;
	}

	@Override
	public void addCart(CartVO cvo) {
		System.out.println("service =="+cvo);
		int count=0;
		count=itemDAO.countCart(cvo.getItemNo());
		System.out.println("넘어온 conut값은 == "+count);
		if(itemDAO.countCart(cvo.getItemNo())==0){
			itemDAO.addCart(cvo);
		}else{
			System.out.println("장바구니에 이미 존재하여, 장바구니에 들어가지 않음.");
		}
	}

	@Override
	public void postResult(PostVO pvo) {
		itemDAO.postResult(pvo);		
	}

	@Override
	public List<PostVO> getPostHuki(int itemNo) {
		return itemDAO.getPostHuki(itemNo);
	}
	public void insertToPurchasedList(String memberId , int itemNo){
		ItemVO ivo = itemDAO.getItemContent(itemNo);
		ivo.setMemberId(memberId);
		itemDAO.insertToPurchasedList(ivo);
		System.out.println("구매목록 등록 완료");
	}
	
	public List<ItemVO> getPurchasedList(String memberId){
		return itemDAO.getPurchasedList(memberId);
	}
	


	@Override
	public void totalPay(String memberId,int total){
		HashMap map=new HashMap();
		map.put("memberId", memberId);
		map.put("total", total);
		itemDAO.totalPay(map);
		
	}

	/*@Override
	public void pointUp(String memberId, int pointUp, String selId) {
		HashMap map=new HashMap();
		if(memberId==memberId&&memberId==selId){
			map.put("memberId", memberId);
			System.out.println("service 1=="+map);
			map.put("memberId", selId);
			System.out.println("service 2=="+map);


		map.put("pointUp", pointUp);
		itemDAO.pointUp(map);
		}
	}*/ //필요 없음
	public int getAverageEvaluation(int itemNo){
		return itemDAO.getAverageEvaluation(itemNo);
	}
	public void sendMessage(MessageVO messvo){
		itemDAO.sendMessage(messvo);
	}
	
	@Override
	public List<CartVO> getCartList(String memberId) {
		return itemDAO.getCartList(memberId);
	}
	@Override
	public ItemVO getCartInfoList(int itemNo) {
		System.out.println("service =="+itemNo);
		return itemDAO.getCartInfoList(itemNo);
	}
	@Override
	public void postDelete(PostVO pvo) {
		itemDAO.postDelete(pvo);
	}
	@Override
	public List<ItemVO> showUploadList(String memberId){
		return itemDAO.showUploadList(memberId);
	}

	@Override
	@Transactional
	public void updateMoneyAndPoint(String memberId, int itemNo) throws Exception { 		
		ItemVO ivo = itemDAO.getItemContent(itemNo);
		itemDAO.withdraw(memberId, ivo.getItemPrice());
		int depositMoney = (int)(ivo.getItemPrice() * 0.8);
		itemDAO.deposit(ivo.getMemberId(), depositMoney);
		System.out.println("금액 입 출금 완료");

		
		
		
	}

	@Override
	public void cartDelete(int no) {
		itemDAO.cartDelete(no);
		
	}


}
